package com.study.mapstruct.mapper;

import com.study.mapstruct.entity.Course;
import com.study.mapstruct.entity.Student;
import com.study.mapstruct.entity.StudentVO;
import org.mapstruct.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@DecoratedWith(StudentMapperDecorator.class)
public interface StudentMapper {

    /*最简单使用*/
    @Mapping(source = "gender.name", target = "gender")
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    StudentVO student2StudentVO(Student student);

    /* list转换 */
    @InheritConfiguration(name = "student2StudentVO")
    //继承配置，不加，属性映射基于上面的mapping配置
    List<StudentVO> students2StudentVOs(List<Student> studentList);

    /* 多对象转换到一个对象 */
    @Mappings({
            @Mapping(source = "student.gender.name", target = "gender"),
            @Mapping(source = "student.birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss"),
            @Mapping(source = "course.courseName", target = "course", defaultValue = "嘿嘿嘿")})
    StudentVO studentAndCourse2StudentVO(Student student, Course course);

    /**
     * expression属性的使用,用于类型转换等。
     * 注意：
     * 1.有了expression则不需要source了
     * 2.expression中表达式必须用java()括起来
     * 3.代码有问题，编译期报错，说明是运行前生成实现类代码，事实也是如此
     */
    @Mapping(target = "gender", expression = "java(com.study.mapstruct.common.GenderEnum.getByName(vo.getGender()))")
    @Mapping(target = "birthday", expression = "java(com.study.mapstruct.common.DateTransform.strToDate(vo.getBirthday()))")
    Student studentVO2Student(StudentVO vo) throws Exception;

    /**
     * 更新现有的Bean实例
     * 注意：可以有返回值，返回值必须是更新后的目标对象。
     */
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    StudentVO updateStudentVOFromStudent(Student student, @MappingTarget StudentVO studentVO);

    /**
     * 继承反转配置,gender属性不能反转，所以忽略
     */
    @Mapping(target = "gender", ignore = true)
    @InheritInverseConfiguration(name = "student2StudentVO")
    Student studentVO2StudentInverse(StudentVO vo);

    /* 映射map */
    @MapMapping(valueDateFormat = "dd.MM.yyyy")
    Map<String, String> longDateMapToStringStringMap(Map<Long, Date> source);

}
